const { buildingModel ,HomeModel} = require('../modules/buildingModel');

async function addbuilding(req,res,next){
    const data=req.body;
    await buildingModel.create(data);
    res.send({
        message: '新增楼栋成功',
        code: 200,

    })
}

async function getBuilding(req,res,next){
    const community_id=req.query;
    console.log(community_id);
    const result=await buildingModel.find(community_id);
    if(result.length>0){
        res.send({
            message:'楼宇数据获取成功',
            code:200,
            result
        })
    }else{
        res.send({
            message:'楼宇数据获取失败',
            code:500,

        })
    }
}
async function getHome(req,res,next){
    const result=await HomeModel.find();
    if(result.length>0){
        res.send({
            message:'房屋数据获取成功',
            code:200,
            result
        })
    }else{
        res.send({
            message:'房屋数据获取失败',
            code:500,

        })
    }
}

async function addHome(req,res,next){
    const data=req.body;
    const homeNumber=await HomeModel.find({homeNumber:data.homeNumber})
    console.log(homeNumber);
    if(homeNumber.length>0){
        res.send({
            message: '新增房屋失败',
            code: 500,
        })
        
    }else{
        await HomeModel.create(data);
        res.send({
            message: '新增房屋成功',
            code: 200,
        })
    }

    
}






module.exports = {addbuilding,getBuilding,addHome,getHome}